{% extends 'bugs/base.html' %}
{% load staticfiles %}
{% load i18n widget_tweaks %}

{% block title %}New Bug{% endblock %}
{% block extrastyle %}

{% endblock %}
{% block content %}
<nav class="navbar navbar-inverse navbar-fixed-top">
    <div class="container">
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar"
                    aria-expanded="false" aria-controls="navbar">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="{% url 'bugs:index' %}">Software Afair Management System</a>
        </div>
    </div>
</nav>
<div class="container">
    <form action="/newbug/" method="post" accept-charset="utf-8" class="form"
          role="form" enctype="multipart/form-data">
        <legend>New Bug</legend>
        {% csrf_token %}
        <div class="form-group">
            <label class="col-sm-2 control-label" for="id_project">专案</label>
            <div class="col-sm-10">
                <select class="form-control" id="id_project" name="project" onchange="auto_select()">
                    {% for project in project_data %}
                    {% if project.type %}
                    <option value="{{ project.name }}--{{ project.type }}">{{ project.name }}--{{ project.type }}
                    </option>
                    {% else %}
                    <option value="{{ project.name }}">{{ project.name }}</option>
                    {% endif %}
                    {% endfor %}
                </select>
            </div>
        </div>

        {% for field in form %}
        {% if field.errors %}
        <div class="form-group has-error">
            <label class="col-sm-2 control-label"
                   for="id_{{ field.name }}">{{ field.label }}</label>
            <div class="col-sm-10">
                {{ field|attr:"class:form-control" }}
                <span class="help-block">
					{% for error in  field.errors %}{{ error }}{% endfor %}
				</span>
            </div>
        </div>
        {% else %}
        <div class="form-group">

            <label class="col-sm-2 control-label"
                   for="id_{{ field.name }}">{{ field.label }}</label>
            <div class="col-sm-10">
                {{ field|attr:"class:form-control" }}
                {% if field.help_text %}
                <p class="help-block">
                    <small>{{ field.help_text }}</small>
                </p>
                {% endif %}
            </div>
        </div>
        {% endif %}
        <br>
        <br>
        {% endfor %}
        <br>

        <div class="form-group">

            <label class="col-sm-2 control-label" for="id_image">图片</label>
            <div class="col-sm-10">
                <input class="form-control" id="id_image" name="image" type="file" multiple="multiple">

            </div>
        </div>

        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <br>
                <button type="submit" class="btn btn-primary">{% trans "Submit" %}</button>
            </div>
        </div>
    </form>
</div>
<script type="text/javascript">
    var p_data = {{ p_data | safe }};

    function auto_select() {
        var selected_pro = $("#id_project option:selected").val();
        // console.log('selected :', selected_pro);
        var this_dri;
        for (let i = 0; i < p_data.length; i++) {
            if (selected_pro == p_data[i].name) {
                this_dri = p_data[i].DRI;
                // console.log('aaa:',i, this_dri);
                break;
            } else if (selected_pro == (p_data[i].name + '--' + p_data[i].type)) {
                this_dri = p_data[i].DRI;
                // console.log('bbb:',i, this_dri);
                break;
            }
        }

        var assi_select = $("#id_assignee").children();
        for (let j = 0; j < assi_select.length; j++) {
            if (this_dri == assi_select[j].value) {
                $("#id_assignee").val(this_dri);
            }
        }
    }

    auto_select();

</script>
{% endblock %}

